package com.example.ecas.controller;

import com.example.ecas.common.CommonResponse;
import com.example.ecas.persistence.DisRtagMapper;
import com.example.ecas.persistence.DiseaseMapper;
import com.example.ecas.pojo.DisRtag;
import com.example.ecas.service.WordCloudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Controller
public class WordCloudController {

    @Autowired
    WordCloudService wordCloudService;

    @Autowired
    DisRtagMapper disRtagMapper;

    // 获取某一病类的词云图
    // 前端通过在html中设置标签 <img src="data:image/png;base64,###"> 来展示词云图片(把###替换成函数返回的字符串)
    @GetMapping(value = "/disWordCloud")
    @ResponseBody
    public CommonResponse<Object> disWordCloud(@RequestParam("diseaseId") String id) {
        int disId;
        try {
            disId = Integer.parseInt(id);
        } catch (Exception e) {
            return CommonResponse.creatForError("无效的病类ID类型");
        }
        List<DisRtag> list = disRtagMapper.getDisRtagByDis(disId);
        if (list.isEmpty()) {
            return CommonResponse.creatForError("不存在查询病类的关系数据");
        }
        try {
            String wcB64 =  wordCloudService.getWordCloudBase64(list);
            return CommonResponse.creatForSuccess("获取词云成功", wcB64);
        } catch (Exception e) {
            return CommonResponse.creatForError("生成词云图失败");
        }
    }

    // 获取某一病历的词云图
//    @GetMapping(value = "/caseWordCloud")
//    @ResponseBody
//    public CommonResponse<Object> caseWordCloud(@RequestParam("caseId") String id) {
//
//    }
}
